<?php
// 验证码类

/* 
// ========================== 文件说明 ==========================//
// 文件说明：PHP验证码(PHP校验码)程序
// --------------------------------------------------------------//
// 程序作者：Sindo（锐利工作室）
// --------------------------------------------------------------//
// 程序版本：0.1
// --------------------------------------------------------------//
// 程序主页：<a href="http://www.wreny.com" target="_blank">http://www.wreny.com</a>
// --------------------------------------------------------------//
// Copyright (C) Wreny Studio. All Rights Reserved.
// ==============================================================//
吕海鹏修订20100602
调用方式
<img src="../upalace_library/upalace.util.validatorCode.php" width="85" height="20" onClick="this.src+='?rand='+Math.random();" title="看不清？单击此处刷新" style="cursor:pointer"/>
或
<img src="../upalace_library/upalace.util.validatorCode.php?width=85&height=20&font=5" width="85" height="20" onClick="this.src+='??width=85&height=20&font=5&rand='+Math.random();" title="看不清？单击此处刷新" style="cursor:pointer"/>
 谭震整合入thinkphp 20121107
 *
 调用方式
 <img src="{:U('lib/code/index',array('font'=>5))}" width="85" height="20" onClick="this.src+='?rand='+Math.random();" title="看不清？单击此处刷新" style="cursor:pointer"/>
  
 */
    //随机字符串种子，可以换成字母或其他英文字符
class CodeAction extends BaseAction {
    public function __construct() {
        parent::__construct();
        
    }
    public function index(){
        $this->glbVerifySeed = "123456789";
        $this->main();
    }
    private function main() {
            session_start();

            $verifyCode = $this->getRandomCode();
            $_SESSION["checkCode"] = $verifyCode;

                    $imgWidth = isset($_GET["width"])?$_GET["width"] : 85;
            $imgHeight = isset($_GET["height"])?$_GET["height"] : 30;
            $imgFont = isset($_GET["font"])?$_GET["font"] : 5;	// 1- 5 使用内置字体。 

                    if($imgWidth =="")		$imgWidth = 85;
                    if($imgHeight == "")	$imgHeight = 30;
                    if($imgFont == "")		$imgFont = 5;

            $this->doOutputImg($verifyCode, $imgWidth, $imgHeight, $imgFont,array(mt_rand(0,100),mt_rand(0,150),mt_rand(0,200) ) );
        }
        //获取随机数字字符串
        private function getRandomCode($length=4) {
            $glbVerifySeed=  $this->glbVerifySeed;

            $bgnIdx = 0;
            $endIdx = strlen($glbVerifySeed)-1;

            $code = "";
            for($i=0; $i<$length; $i++) {
                $curPos = rand($bgnIdx, $endIdx);
                $code .= substr($glbVerifySeed, $curPos, 1);
            }

            return $code;
        }

        //输出校验码图像
        function doOutputImg($string, $imgWidth, $imgHeight, $imgFont,
            $imgFgColorArr=array(0,0,0), $imgBgColorArr=array(255,255,255)) {
            $image = imagecreatetruecolor($imgWidth, $imgHeight);

            //用白色背景加黑色边框画个方框
            $backColor = imagecolorallocate($image, 255, 255, 255);
            $borderColor = imagecolorallocate($image, 0, 0, 0);
            imagefilledrectangle($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $backColor);
            imagerectangle($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $borderColor);

            $imgFgColor = imagecolorallocate ($image, $imgFgColorArr[0], $imgFgColorArr[1], $imgFgColorArr[2]);
            $this->doDrawStr($image, $string, $imgFgColor, $imgFont);
            $this->doPollute($image, 200);

            header('Content-type: image/png');
            imagepng($image);
            imagedestroy($image);
        }

        //画出校验码
        private function doDrawStr($image, $string, $color, $imgFont) {
            $imgWidth = imagesx($image)-4;
            $imgHeight = imagesy($image);

            $count = strlen($string);
            $xpace = ($imgWidth/$count);

            $x = ($xpace-6)/2+2;
            $y = ($imgHeight/2-8);
            for ($p = 0; $p<$count;  $p ++) {
                $xoff = rand(-2, +2);
                $yoff = rand(-5, +5);
                $curChar = substr($string, $p, 1);
                imagestring($image, $imgFont, $x+$xoff, $y+$yoff, $curChar, $color);
                $x += $xpace;
            }

            return 0;
        }

        //画出一些杂点
        private function doPollute($image, $times) {   
            $imgWidth = imagesx($image);
            $imgHeight = imagesy($image);
            for($j=0; $j<$times; $j++) {
                $x = rand(0, $imgWidth);
                $y = rand(0, $imgHeight);

                $color = imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255));
                imagesetpixel($image, $x, $y, $color);
            }
        }
    
    
}